﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 文件名: TrAttach.cs
// 作 者：代码自动生成
// 日 期：2011-11-7 11:47:49
// 描 述：
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using Csla;
using lite;
using Csla.Validation;
using Csla.Data;
using System.Data.SqlClient;
using ZX.DAL;
using OpenExpressApp.ORM;

namespace ZX.TR.Library
{
   

       /// <summary>
    /// 
    /// </summary>
	[Serializable]
    [Table(Name="V_TrCourse")]
    [ZX.DBModule.Table("V_TrCourse","TrCourseID")]
	public class V_TrCourse: ZX.DBModule.BaseEntity
	{
        
        #region Business Methods 业务方法
        
        /// <summary>
        /// 附件ID
        /// </summary>
        private static PropertyInfo<Guid> TrAttachIDProperty = RegisterProperty(typeof(V_TrCourse), new PropertyInfo<Guid>("TrAttachID"));
        [DataObjectField(true, true)]
        [Column, PK]
        public Guid TrAttachID
        {
            get { return GetProperty(TrAttachIDProperty); }
            set { SetProperty(TrAttachIDProperty, value); }
        }
		/// <summary>
        /// 课程ID
        /// </summary>
        private static PropertyInfo< Guid > TrCourseIDProperty = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< Guid >("TrCourseID"));
        [DataObjectField(true, true)]
        [Column, PK]
        public Guid TrCourseID
        {
            get { return GetProperty(TrCourseIDProperty); }
            set { SetProperty(TrCourseIDProperty, value); }
        }
        
		/// <summary>
        /// 课程编码
        /// </summary>
        private static PropertyInfo< string > CourseCodeProperty = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< string >("CourseCode"));
        [Column]
        public string CourseCode
        {
            get { return GetProperty(CourseCodeProperty); }
            set { SetProperty(CourseCodeProperty, value); }
        }
        /// <summary>
        /// 资料名称
        /// </summary>
        private static PropertyInfo<string> AttachNameProperty = RegisterProperty(typeof(V_TrCourse), new PropertyInfo<string>("AttachName"));
        [Column]
        public string AttachName
        {
            get { return GetProperty(AttachNameProperty); }
            set { SetProperty(AttachNameProperty, value); }
        }
        /// <summary>
        /// 资料地址
        /// </summary>
        private static PropertyInfo<string> AttachAddressProperty = RegisterProperty(typeof(V_TrCourse), new PropertyInfo<string>("Address"));
        [Column]
        public string Address
        {
            get { return GetProperty(AttachAddressProperty); }
            set { SetProperty(AttachAddressProperty, value); }
        }
		/// <summary>
        /// 课程名称
        /// </summary>
        private static PropertyInfo< string > CourseNameProperty = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< string >("CourseName"));
        [Column]
        public string CourseName
        {
            get { return GetProperty(CourseNameProperty); }
            set { SetProperty(CourseNameProperty, value); }
        }
		/// <summary>
        /// 课程类型
        /// </summary>
        private static PropertyInfo< string > CourseTypeProperty = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< string >("CourseType"));
        [Column]
        public string CourseType
        {
            get { return GetProperty(CourseTypeProperty); }
            set { SetProperty(CourseTypeProperty, value); }
        }
		/// <summary>
        /// 课程费用
        /// </summary>
        private static PropertyInfo< string > CourseCostProperty = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< string >("CourseCost"));
        [Column]
        public string CourseCost
        {
            get { return GetProperty(CourseCostProperty); }
            set { SetProperty(CourseCostProperty, value); }
        }
		/// <summary>
        /// 课程内容
        /// </summary>
        private static PropertyInfo< string > CourseContentProperty = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< string >("CourseContent"));
        [Column]
        public string CourseContent
        {
            get { return GetProperty(CourseContentProperty); }
            set { SetProperty(CourseContentProperty, value); }
        }
		/// <summary>
        /// 课程目的
        /// </summary>
        private static PropertyInfo<string> CourseAimProperty = RegisterProperty(typeof(V_TrCourse), new PropertyInfo<string>("CourseAim"));
        [Column]
        public string CourseAim
        {
            get { return GetProperty(CourseAimProperty); }
            set { SetProperty(CourseAimProperty, value); }
        }
		/// <summary>
        /// 需用课时
        /// </summary>
        private static PropertyInfo< decimal > NeedHourProperty = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< decimal >("NeedHour"));
        [Column]
        public decimal NeedHour
        {
            get { return GetProperty(NeedHourProperty); }
            set { SetProperty(NeedHourProperty, value); }
        }
		/// <summary>
        /// 最小开班人数
        /// </summary>
        private static PropertyInfo< int > MinStudentsProperty = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< int >("MinStudents"));
        [Column]
        public int MinStudents
        {
            get { return GetProperty(MinStudentsProperty); }
            set { SetProperty(MinStudentsProperty, value); }
        }
		/// <summary>
        /// 最大开班人数
        /// </summary>
        private static PropertyInfo< int > MaxStudentsProperty = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< int >("MaxStudents"));
        [Column]
        public int MaxStudents
        {
            get { return GetProperty(MaxStudentsProperty); }
            set { SetProperty(MaxStudentsProperty, value); }
        }
		/// <summary>
        /// 课程条件
        /// </summary>
        private static PropertyInfo< string > CourseConditionProperty = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< string >("CourseCondition"));
        [Column]
        public string CourseCondition
        {
            get { return GetProperty(CourseConditionProperty); }
            set { SetProperty(CourseConditionProperty, value); }
        }
		/// <summary>
        /// 是否公开
        /// </summary>
        private static PropertyInfo<string> IsPublicProperty = RegisterProperty(typeof(V_TrCourse), new PropertyInfo<string>("IsPublic"));
        [Column]
        public string IsPublic
        {
            get { return GetProperty(IsPublicProperty); }
            set { SetProperty(IsPublicProperty, value); }
        }
		/// <summary>
        /// 是否公用
        /// </summary>
        private static PropertyInfo<string> IsCommunalProperty = RegisterProperty(typeof(V_TrCourse), new PropertyInfo<string>("IsCommunal"));
        [Column]
        public string IsCommunal
        {
            get { return GetProperty(IsCommunalProperty); }
            set { SetProperty(IsCommunalProperty, value); }
        }
		/// <summary>
        /// 说明
        /// </summary>
        private static PropertyInfo< string > ExplainProperty = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< string >("Explain"));
        [Column]
        public string Explain
        {
            get { return GetProperty(ExplainProperty); }
            set { SetProperty(ExplainProperty, value); }
        }
		/// <summary>
        /// 操作员编号
        /// </summary>
        private static PropertyInfo< Guid > UserIdProperty = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< Guid >("AddUser"));
        [Column]
        public Guid AddUser
        {
            get { return GetProperty(UserIdProperty); }
            set { SetProperty(UserIdProperty, value); }
        }
		/// <summary>
        /// 新增时间
        /// </summary>
        private static PropertyInfo< DateTime > AddtimeProperty = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< DateTime >("Addtime"));
        [Column]
        public DateTime Addtime
        {
            get { return GetProperty(AddtimeProperty); }
            set { SetProperty(AddtimeProperty, value); }
        }
		/// <summary>
        /// 备用字段1
        /// </summary>
        private static PropertyInfo< string > Exp1Property = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< string >("Exp1"));
        [Column]
        public string Exp1
        {
            get { return GetProperty(Exp1Property); }
            set { SetProperty(Exp1Property, value); }
        }
		/// <summary>
        /// 备用字段2
        /// </summary>
        private static PropertyInfo< string > Exp2Property = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< string >("Exp2"));
        [Column]
        public string Exp2
        {
            get { return GetProperty(Exp2Property); }
            set { SetProperty(Exp2Property, value); }
        }
		/// <summary>
        /// 备用字段3
        /// </summary>
        private static PropertyInfo< string > Exp3Property = RegisterProperty(typeof(V_TrCourse),new PropertyInfo< string >("Exp3"));
        [Column]
        public string Exp3
        {
            get { return GetProperty(Exp3Property); }
            set { SetProperty(Exp3Property, value); }
        }
		
        #endregion
        
        #region Validation Rules 验证规则
        //将验证规则与业务对象的属性联系在一起
        protected override void AddBusinessRules()
        {
            //AddRule的第一个参数是委托变量，也就是说是方法的变量
            ValidationRules.AddRule(CommonRules.StringRequired, "CourseCode");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CourseCode", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "CourseName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CourseName", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "CourseType");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CourseType", 50));
            ValidationRules.AddRule(CommonRules.StringRequired, "CourseCost");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CourseCost", 300));
            ValidationRules.AddRule(CommonRules.StringRequired, "CourseContent");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CourseContent", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "CourseCondition");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CourseCondition", 300));
            ValidationRules.AddRule(CommonRules.StringRequired, "Explain");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Explain", 300));
            ValidationRules.AddRule(CommonRules.StringRequired, "Exp1");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Exp1", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "Exp2");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Exp2", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "Exp3");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Exp3", 30));
            //base.AddBusinessRules();
        }
        #endregion
        
        #region Factory Methods 工厂方法

        public V_TrCourse(){}

        public static V_TrCourse New()
        {
            return DataPortal.Create< V_TrCourse>();
        }
        public static V_TrCourse Get(Guid TrCourseID)
        {
            return DataPortal.Fetch< V_TrCourse>(new SingleLazyCriteria< V_TrCourse, Guid>(TrCourseID, false));
        }
        public static void Delete(Guid TrCourseID)
        {
            DataPortal.Delete(TrCourseID);
        }

        //public override V_TrCourse Save()
        //{
            //return base.Save();
        //}
        #endregion
		
		#region Data Access 数据访问
        //由于DataPortal_create中没有对数据库的访问，所有标记了Runlocal特性，使得该方法只需在本地运行即可
        //如果代码中有访问数据库的方法，那么就不要用这个特性，使数据门户可以根据情况将此方法的运行选择在应用服务器还是本地
        [RunLocal()]
        protected override void DataPortal_Create()
        {
            //在此调用CheckRules对所有的属性进行验证规则的检查，这样检查只运行一次。
            ValidationRules.CheckRules();
        }


        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void DataPortal_Fetch(Guid TrCourseID)
        {
            //通过连接字符串建立SQL连接对象，Using符号代表，范围释放的意思，SqlConnection对象会在此范围后被释放掉
             using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("TrCourseID").Equal(TrCourseID);
                var data = db.Select< V_TrCourse>(q)[0];
                LoadProperty(TrAttachIDProperty, data.TrAttachID);
                LoadProperty(TrCourseIDProperty, data.TrCourseID);
                LoadProperty(CourseCodeProperty, data.CourseCode);
                LoadProperty(CourseNameProperty, data.CourseName);
                LoadProperty(CourseTypeProperty, data.CourseType);
                LoadProperty(CourseCostProperty, data.CourseCost);
                LoadProperty(AttachAddressProperty, data.AttachName);
                LoadProperty(AttachAddressProperty, data.Address);
                LoadProperty(CourseContentProperty, data.CourseContent);
                LoadProperty(CourseAimProperty, data.CourseAim);
                LoadProperty(NeedHourProperty, data.NeedHour);
                LoadProperty(MinStudentsProperty, data.MinStudents);
                LoadProperty(MaxStudentsProperty, data.MaxStudents);
                LoadProperty(CourseConditionProperty, data.CourseCondition);
                LoadProperty(IsPublicProperty, data.IsPublic);
                LoadProperty(IsCommunalProperty, data.IsCommunal);
                LoadProperty(ExplainProperty, data.Explain);
                LoadProperty(UserIdProperty, data.AddUser);
                LoadProperty(AddtimeProperty, data.Addtime);
                LoadProperty(Exp1Property, data.Exp1);
                LoadProperty(Exp2Property, data.Exp2);
                LoadProperty(Exp3Property, data.Exp3);
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            //依然是调用下面的方法删除的，只是他此时可以获得自己的Id
            DataPortal_Delete(TrCourseIDProperty);
        }
        #endregion

	}
}